package cn.com.customer.manager.ui.fragment.me

import android.content.Intent
import androidx.appcompat.app.AlertDialog
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.DialogWaysAdapter
import cn.com.customer.manager.application.App
import cn.com.customer.manager.base.BaseFragment
import cn.com.customer.manager.bean.User
import cn.com.customer.manager.manager.LoginManager
import cn.com.customer.manager.ui.center.about.AboutUsActivity
import cn.com.customer.manager.ui.edit.EditInfoActivity
import cn.com.customer.manager.ui.regist.FindPasswordActivity
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
import com.xuexiang.xui.widget.imageview.strategy.ILoadListener
import com.xuexiang.xui.widget.imageview.strategy.impl.GlideImageLoadStrategy
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.fragment_mine.*


class MineFragment : BaseFragment(),MineContact.View{
    private var mList: ArrayList<String> = ArrayList()
    override fun getLayoutId(): Int = R.layout.fragment_mine
    private lateinit var presenter: MineContact.Presenter
    private var user: User? =LoginManager.getInstance(App.getContext()).user
    override fun initView() {

        MinePresenter(this)
        user?.let {
            if (!it.headUrl.isNullOrEmpty()){
            GlideImageLoadStrategy().loadImage(iv_photo,it.headUrl,object : ILoadListener{
                override fun onLoadFailed(error: Throwable?) {
                    iv_photo.setImageResource(R.drawable.photo_icon)
                }
                override fun onLoadSuccess() {
                }

            })   }
            tv_name.text = it.name
            if (!it.deptName.isNullOrEmpty()){
                tv_role.text=it.deptName
            }
            tv_phone.text=it.mobile
            tv_message.text=it.email
        }
        switch_btn.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                ToastUtils.toast(getString(R.string.mine_close))
            } else {
                ToastUtils.toast(getString(R.string.mine_open))
            }
        }
        ll_language_select.setOnClickListener {
            selectLanguageWays()
        }
        ll_about_me.setOnClickListener {
            startActivity(Intent(activity, AboutUsActivity::class.java))
        }
        button_exit_login.setOnClickListener {
            MaterialDialog.Builder(context!!)
                    .content(R.string.tip_exit_login)
                    .positiveText(R.string.lab_yes)
                    .negativeText(R.string.lab_no)
                    .contentColorRes(R.color.base_text)
                    .buttonRippleColorRes(R.color.colorPrimary)
                    .onPositive { dialog, which ->
                      presenter.loginOut()
                        dialog.dismiss()
                    }
                    .onNegative{
                        dialog, which ->
                        dialog.dismiss()
                    }
                    .show()
        }
        rl_info.setOnClickListener {
            startActivity(Intent(activity,EditInfoActivity::class.java))
        }
        ll_account_safe.setOnClickListener {
            startActivity(Intent(activity, FindPasswordActivity::class.java))
        }
    }

    /**
     * 选择语言方式
     */
    private fun selectLanguageWays() {
        mList.clear()
        mList.add("中文")
        mList.add("英文")
        AlertDialog.Builder(mContext)
                .setTitle(getString(R.string.select))
                .setAdapter(DialogWaysAdapter(mContext, mList)) { _, i ->
                    tv_china_englinsh.text = mList[i]
                }
                .show()
    }

    override fun loginOut() {

    }

    override fun setPresenter(presenter: MineContact.Presenter) {
        this.presenter=presenter
    }

}